/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BookListPanel.java
 *
 * Created on Mar 30, 2011, 11:16:31 PM
 */

package org.libarae.client;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.JLabel;
import org.libarae.communication.common.Book;

/**
 *
 * @author Ben
 */
@SuppressWarnings("serial")
public class BookListPanel extends javax.swing.JPanel {

    @SuppressWarnings("unused")
	private Vector<Book> mBooks;
    private Vector<BookPanel> mBookPanels;
    private Color selectedBackgroundColor;
    private int selectedIndex = -1;

    private MainScreen mMainScreen;

    /** Creates new form BookListPanel */
    public BookListPanel() {
        initComponents();
    }

    public BookListPanel(MainScreen mMainScreen) {
        this.mMainScreen = mMainScreen;
        
        initComponents();
    }

    public BookListPanel(Vector<Book> mBooks, MainScreen mMainScreen)
    {
        this.mBooks = mBooks;
        this.mMainScreen = mMainScreen;
        
        mBookPanels = new Vector<BookPanel>();

        initComponents();
    }

    public void setBooks(Vector<Book> mBooks)
    {
        removeAll();
        this.mBooks = mBooks;
        mBookPanels = new Vector<BookPanel>();

        if(mBooks != null)
        {
            if(mBooks.size() == 0)
            {
                JLabel lblEmpty = new JLabel("No books found.");
                lblEmpty.setFont(new Font("Tahoma",0,18));
                lblEmpty.setVerticalAlignment(javax.swing.SwingConstants.TOP);
                add(lblEmpty);
            }
            else
            {
                for(Book b : mBooks)
                {
                    mBookPanels.add(new BookPanel(b, mMainScreen));
                    if(mBookPanels != null)
                        add(mBookPanels.get(mBookPanels.size()-1));
                }
            }
        }
    }

    public void setSelectedItem(Object o)
    {
        int newSelectedIndex = mBookPanels.indexOf(o);

        selectItemByIndex(newSelectedIndex);
    }

    public void setSelectedIndex(int i)
    {
        if(i < 0 || i >= mBookPanels.size())
            return;
        selectItemByIndex(i);
    }


    public void selectItemByIndex(int newSelectedIndex)
    {
        if(selectedIndex != -1)
        {
            mBookPanels.get(selectedIndex).setBackground(selectedBackgroundColor);
            mBookPanels.get(selectedIndex).setBorder(null);
        }
        selectedBackgroundColor = mBookPanels.get(newSelectedIndex).getBackground();
        mBookPanels.get(newSelectedIndex).setBackground(Color.LIGHT_GRAY);
        selectedIndex = newSelectedIndex;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLoading = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setAutoscrolls(true);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        setLayout(new java.awt.GridLayout(0, 1));

        lblLoading.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblLoading.setText("Loading...");
        lblLoading.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        add(lblLoading);
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked

}//GEN-LAST:event_formMouseClicked

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed

        switch(evt.getKeyCode())
        {
            case KeyEvent.VK_DOWN:
                // Select BookPanel below the currently selected BookPanel
                if(selectedIndex < mBookPanels.size())
                    setSelectedIndex(++selectedIndex);
                break;
            case KeyEvent.VK_UP:
                // Select BookPanel above the currently selected BookPanel
                if(selectedIndex > 0)
                    setSelectedIndex(--selectedIndex);
                break;
        }
    }//GEN-LAST:event_formKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblLoading;
    // End of variables declaration//GEN-END:variables

}
